package com.sc.nft.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;

import com.sc.nft.annotation.WithoutSign;
import com.sc.nft.dao.UserChainInfoDao;
import com.sc.nft.entity.UserChainInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.service.NoticeInfoService;
import com.sc.nft.service.SecondCollectionService;
import com.sc.nft.service.SecondOrderService;
import com.sc.nft.service.UserCollectionService;
import com.sc.nft.sup.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/ztan")
@Api(tags ="智探API对接接口")
@ApiIgnore
public class ExplorationOfWisdomController {

    private final SecondOrderService secondOrderService;
    private final SecondCollectionService secondCollectionService;
    private final NoticeInfoService noticeInfoService;
    private final UserCollectionService userCollectionService;
    private final UserChainInfoDao userChainInfoDao;
    private static final String SIGN = "mate-ztan";


    @ApiOperation(value = "藏品列表", notes = "藏品列表")
    @GetMapping(value = "/art-list")
    @WithoutSign
    public ApiResult<List<ZhiTanArtListVO>> artList(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                    @RequestParam(value = "page-size",defaultValue = "50") Integer pageSize,
                                                    @RequestParam(value = "time") Integer time,
                                                    @RequestParam(value = "sign") String sign) {
        String md5Hex = DigestUtils.md5Hex(SIGN + time);
        long secondTime = DateUtil.date().getTime() / 1000L;
        if (secondTime - time > 60L){
            return ApiResult.fail("当前时间戳已失效！");
        }
        //校验签名
        if (!md5Hex.equals(sign)){
            return ApiResult.fail("接口签名错误！");
        }
        List<ZhiTanArtListVO> dataList = secondCollectionService.artList(page, pageSize);
        return ApiResult.success(dataList);
    }

    @ApiOperation(value = "交易记录", notes = "交易记录")
    @GetMapping(value = "/trade-list")
    @WithoutSign
    public ApiResult<List<ZhiTanTradeVO>> tradeList(@RequestParam("start-time") Integer startTime,
                                                    @RequestParam("limit") Integer limit,
                                                    @RequestParam("time") Integer time,
                                                    @RequestParam("sign") String sign) {
        String md5Hex = DigestUtils.md5Hex(SIGN + time);
        long secondTime = DateUtil.date().getTime() / 1000L;
        if (secondTime - time > 60L){
            return ApiResult.fail("当前时间戳已失效！");
        }
        //校验签名
        if (!md5Hex.equals(sign)){
            return ApiResult.fail("接口签名错误！");
        }
        return ApiResult.success(secondOrderService.tradeList(startTime, limit));
    }


    @ApiOperation(value = "用户持仓", notes = "用户持仓")
    @GetMapping(value = "/user-hold")
    @WithoutSign
    public ApiResult<List<ZhiTanUserHoldVO>> userHold(@RequestParam("addr") String addr,
                                                      @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                      @RequestParam(value = "page-size",defaultValue = "50") Integer pageSize,
                                                      @RequestParam("time") Integer time,
                                                      @RequestParam("sign") String sign) {
        String md5Hex = DigestUtils.md5Hex(SIGN + time);
        long secondTime = DateUtil.date().getTime() / 1000L;
        if (secondTime - time > 60L){
            return ApiResult.fail("当前时间戳已失效！");
        }
        //校验签名
        if (!md5Hex.equals(sign)){
            return ApiResult.fail("接口签名错误！");
        }
        UserChainInfo user = userChainInfoDao.getByAddress(addr);
        if (ObjectUtil.isNull(user)){
            return ApiResult.fail("不存在此地址的用户");
        }
        List<ZhiTanUserHoldVO> userHold = userCollectionService.userHold(user.getUserId(),page, pageSize);
        return ApiResult.success(userHold);
    }

    @ApiOperation(value = "公告接口", notes = "公告接口")
    @GetMapping(value = "/notice-list")
    @WithoutSign
    public ApiResult<List<ZhiTanNoticeVO>> noticeList( @RequestParam(value = "page",defaultValue = "1") Integer page,
                                                       @RequestParam(value = "page-size",defaultValue = "50") Integer pageSize,
                                                       @RequestParam("time") Integer time,
                                                       @RequestParam("sign") String sign) {
        String md5Hex = DigestUtils.md5Hex(SIGN + time);
        long secondTime = DateUtil.date().getTime() / 1000L;
        if (secondTime - time > 60L){
            return ApiResult.fail("当前时间戳已失效！");
        }
        //校验签名
        if (!md5Hex.equals(sign)){
            return ApiResult.fail("接口签名错误！");
        }
        return ApiResult.success(noticeInfoService.noticeList(page,pageSize));
    }

    @ApiOperation(value = "单个藏品信息", notes = "单个藏品信息")
    @GetMapping(value = "/art-info")
    @WithoutSign
    public ApiResult<ZhiTanArtInfoVO> artInfo(@RequestParam(value = "id") Integer id,
                                              @RequestParam("time") Integer time,
                                              @RequestParam("sign") String sign) {
        String md5Hex = DigestUtils.md5Hex(SIGN + time);
        long secondTime = DateUtil.date().getTime() / 1000L;
        if (secondTime - time > 60L){
            return ApiResult.fail("当前时间戳已失效！");
        }
        //校验签名
        if (!md5Hex.equals(sign)){
            return ApiResult.fail("接口签名错误！");
        }
        ZhiTanArtInfoVO result = secondCollectionService.artInfo(id);
        if (ObjectUtil.isNull(result)){
            return ApiResult.fail("不存在此id的藏品");
        }
        return ApiResult.success(result);
    }
}
